package jml.defaultimple.web;

import java.net.URLDecoder;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jml.config.Config;
import jml.config.Descriptor;
import jml.config.cmd.Command;
import jml.config.cmd.CommandExec;
import jml.config.cmd.CommandGroup;
import jml.def.Library;
import jml.defaultimple.util.JMLPluginManager;

@WebServlet("/edit")
public class ServletEditor extends HttpServlet
{
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
	{
		// recibo el path del title
		String path = request.getParameter("folder");
		String pathOK = URLDecoder.decode(path, "UTF-8");
		
		Descriptor d = Config.getConfig(false).getDescriptor();
		String fileToEdit = d.getDescriptorFilename(pathOK);
		
//		ExternalCommands ec = Config.getConfig(false).getExternalCommands();
//		Editor e=ec.getEditor();
		
		CommandGroup cg = Config.getConfig(false).getCommandGroup();
		Command e = cg.getComnand("EDITOR","default");
		e.getAppParams().put("TITLEPATH",pathOK);
		e.getAppParams().put("JMLFILE",fileToEdit);
		CommandExec.exec(e);
		
		// notifico a los plugins que se acaba de cargar un titulo		
		Library library = (Library) getServletContext().getAttribute("library");
		JMLPluginManager.notifyAll("onEdit", library.getTitleByPath(pathOK));		
		return;
	}
}
